program AddResnum
implicit none

character(len=99)ARGUMENT,INPUTFILE,PDB,symbol(196)
integer i,j,k,ios,c1(196),c2(196),c3(196),c4(196),c5(196),connum1,connum2,nargs
real d1(196),d2(196)
character(len=20),dimension(20) :: col

nargs=iargc()
  if(nargs==2)then 
     call getarg(1,INPUTFILE)
     call getarg(2,PDB)
  else
  write(*,*) " "
  write(*,*) "Wrong Input!"
  write(*,*) "INPUTFILE  PDBFILE!"
  write(*,*) " rNA forTOPca.pdb"
  goto 100
  endif

ios=0
i=0
open(10,file=INPUTFILE,status="old")
do while(.true.)
 read(10,"(A)",iostat=ios)ARGUMENT
 if(ios/=0)exit
  i=i+1
! read(ARGUMENT,*) (col(k),k=1,3)
! read(col(1),"(I4)") c1(i)
! read(col(2),"(F5.3)") d1(i)
! read(col(3),"(A1)") symbol(i)
  read(ARGUMENT(2:4),"(I3)") c1(i)
  read(ARGUMENT(8:12),"(F5.3)") d1(i)
  read(ARGUMENT(16:16),"(A1)") symbol(i)
enddo
close(10)
connum1=i
!  write(*,*) connum1

ios=0
i=0
open(20,file=PDB,status="old")
do while(.true.)
 read(20,"(A)",iostat=ios)ARGUMENT
 if(ios/=0)exit
 i=i+1
 read(ARGUMENT(24:26),"(I3)") c3(i)
enddo
close(20)

open(30,file="RNAs.dat",status="replace")
do i=1,connum1
  write(30,"(1X,I4,3X,F5.3,3X,A1,3X,I4)") c1(i),d1(i),symbol(i),c3(c1(i))
enddo
close(30)

100 end program
